//
//  FourthViewController.m
//  MyProject
//
//  Created by apple on 15/10/30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "FourthViewController.h"
#import "AFNetworking.h"
#import "MeiShiModel.h"
#import "UIImageView+AFNetworking.h"
#import "MeiShiSViewController.h"
#import "MeiShiDetailViewController.h"
#import "MMProgressHUD.h"
#define SCREENSIZE [UIScreen mainScreen].bounds.size
//#define  URL2 @"http://www.youyuwei.com/api/recommend?ywsdk_ver=20131018&app_code=com.youyuwei.yuwei.ios"
#define URL2 @"http://www.youyuwei.com/api/recommend?ywsdk_ver=20131018&app_code=com.youyuwei.yuwei.ios&oauth_timestamp=1447055479&oauth_version=1.0&oauth_nonce=05e82eb61191b10a896f8068ed60d3c1&oauth_signature=bjTwJIX2%2FrOr117Ta0SIRKzrNWE%3D&sys_ver=8.1.2&hardware_model=iPhone6%2C2&oauth_consumer_key=4&device_token=&ver=2&oauth_signature_method=HMAC-SHA1&debug=0&x_auth_mode=client_auth&app_ver=2.1&oauth_token=0_9837387abc30183c39&channel_id=App%20Store&start=0&open_udid=a8fcf546a16505b727cb757163ce7b12b209f951&device_type=ios&idfa=60CAFE04-71F1-4A24-A54D-4CAA8EF805E2"
#define WIDTH ((self.view.bounds.size.width)/2)

@interface FourthViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation FourthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArr = [[NSMutableArray alloc]initWithCapacity:0];
    self.title = @"爱上美食";
    [self getData];
    [self createView];
    [self createNav];
}


- (void)createNav{
    //设置navigationBar属性
    NSDictionary * attributeDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:23],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributeDic];
    
    //设置navigationBar颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHue:20/255.f saturation:190/255.f brightness:20/255.f alpha:1];
}

-(void)getData
{
    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleDrop];
    //显示 特效 设置 标题
    [MMProgressHUD showWithTitle:@"下载" status:@"Loading..."];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    __weak typeof(self)weakSelf = self;
    [manager GET:URL2 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil][@"data"];
        NSArray * arr = dic[@"list"];
        
        NSDictionary * subDic = [arr objectAtIndex:2];
        
        NSArray * itemArr = subDic[@"content"];
        
        
        for (NSDictionary *itemDic in itemArr) {
            MeiShiModel *model = [[MeiShiModel alloc]initWithDictionary:itemDic error:nil];
            if (!model) {
                continue;
            }
            [weakSelf.dataArr addObject:model];
        }
        [weakSelf.collectionView reloadData];
        [MMProgressHUD dismissWithSuccess:@"下载完成" title:@"ok"];

        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

-(void)createView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 5;
    layout.itemSize = CGSizeMake(WIDTH, WIDTH+30);
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"MeishiCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"120"];
    
    self.collectionView.backgroundColor  =[UIColor whiteColor];
}


#pragma mark 代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MeiShiModel *model = [_dataArr objectAtIndex:indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"120" forIndexPath:indexPath];
    UIImageView *imgV = (UIImageView *)[cell.contentView viewWithTag:1000];
    [imgV setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
    UILabel *lable = (UILabel *)[cell.contentView viewWithTag:1001];
    lable.text = model.name;
    
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(WIDTH, WIDTH );
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MeiShiModel *model = [_dataArr objectAtIndex:indexPath.row];
    if (model.state.length) {
        MeiShiSViewController *meishi = [[MeiShiSViewController alloc]init];
        meishi.namea = model.name;
        meishi.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:meishi animated:YES];
    }else{
    
        MeiShiDetailViewController *meiShiDetail = [[MeiShiDetailViewController alloc]init];
        meiShiDetail.ID = model.id;
        meiShiDetail.titleName = model.name;
        meiShiDetail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:meiShiDetail animated:YES];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
